/*TCSS 360 ConferenceGuru
 * 
 * Aaron Kaufman
 * Latest Edit:  12/2/2012
 * 
 * Edited by: Darrion Harris, Lawrence Grass
 */
package model;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Observable;
import java.util.TreeMap;

public class Portal extends Observable
					implements Serializable
					
{
    
    /*
     * Controls saving and loading conferences.
     * Also responsible for loading the default conference in cases
     * where no save file is available.
     */
    private static final long serialVersionUID = 1L;
    private Collection<Conference> my_conferences;
    
    public Portal(Collection<Conference> the_confs)
    {
        my_conferences = the_confs;
    }
    
    public Portal()
    {
        my_conferences = new ArrayList<Conference>();
    }
    
    //Returns the conference in the list with the given title; or, 
    //returns null if no such conference exists.
    public Conference getConference(String title)
    {
        for (Conference c: my_conferences)
        {
            if (c.getConfTitle().equals(title))
            {
                return c;
            }
        }
        return null;
    }
    
    
    public Collection<Conference> getAllConferences()
    {
        return my_conferences;
    }
    
    public void addConference(Conference the_conference) {
    	my_conferences.add(the_conference);
    	setChanged();
    	notifyObservers(this);
    }
    
    /*
     * Pre: No conference can exist with the same name as the created conference.
     * Post:  Creates a new conference (titled the_title) with the_user
     *  as the program chair of the conference.
     * 
     */
    public void addConference(String the_user, String the_title)
    {
        ProgramChair new_prog_chair = new ProgramChair(
            new ArrayList<ConferencePaper>(),
        	new TreeMap<String, SubProgramChair>(), 
        	new TreeMap<String, Reviewer>(),
        	the_user);
        Conference new_conf = new Conference(the_title, new_prog_chair, the_user);
        my_conferences.add(new_conf);
        setChanged();
        notifyObservers(this);
    }
    
    
    /*
     * Pre:  None.
     * Post:  Saves the serialized Portal to disk under the_filename.
     */
    public void save(String the_filename)
    {
       try{
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(the_filename));
        oos.writeObject(this);
        oos.flush();
        oos.close();
       }
       catch(IOException i)
       {
           i.printStackTrace();
       }
    }
    
    public String toString()
    {
        StringBuilder s = new StringBuilder("");
        for (Conference c: this.getAllConferences())
        {
            s.append(c.toString());
        }
        return s.toString();
    }
    //Loads the portal, if one exists in the specified location;
    //otherwise, returns whatever our "default" portal is.
    // Edited By: Darrion Harris,
    public static Portal load(String the_filename)
    {
        
        
        ObjectInputStream ois;
        try
        {
            ois = new ObjectInputStream(new FileInputStream(the_filename));
            Portal new_portal;

            new_portal = (Portal) ois.readObject();
            

            ois.close();
            return new_portal;
        } 
        catch (IOException i)
        {
            //If we can't find the portal, just return the default one.
            return getDefaultPortal();
        }
        catch (ClassNotFoundException e){
            throw new IllegalStateException("NOOOOOOOOOOOOOOOOOOOO");
        }
        
    }
    
    
    //pre:  None
    //Post: Creates a Portal with prepopulated conferences in it.
    // Edited By: Darrion Harris, Lawrence Grass
    public static Portal getDefaultPortal()
    {
        Portal default_portal = new Portal();
        
        //Insert whatever we want the new portal to have as its attributes.
        Conference conf = new Conference("Compu Con 2013", "Thomas Watson");
        conf.setDeadline("4/30/2013");
        conf.designateReviewer("Darrion Harris");
        conf.getProgramChair().designateSubprogramChair("Darrion Harris");
        SubProgramChair sub_chair = conf.getProgramChair().getSubprogramChairs().get("Darrion Harris");
        
        /*
         * New ConferencePaper Parameters:
         * 
         * String the_author, 
         * String the_title, 
         * String the_manuscript_file, 
         * String the_user,
         * String the_email,  // not displayed by gui
         * String the_category_of_paper, // not displayed by gui
         * String the_keywords, // not displayed by gui
         * String the_abstract // not displayed by gui
         * 
         */
        conf.addPaperToConference(
        		"Darrion Harris", "Statistical Machine Learning", 
        		"once apon a time there was a bee", "Darrion Harris", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Ulysees C Irving", "The Data killer", 
        		"once apon a time there was a bee", "Aaron", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Tyler Redding", "Boundary Between Hardware and Software", 
        		"once apon a time there was a bee", "Tyler", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Stan G", "Intro to Java", 
        		"once apon a time there was a bee", "Stan Gerber", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Bill Gates", "Computer Screens and eye Development", 
        		"once apon a time there was a bee", "Bill Gates", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Bill Gates", "How I Became A Millionaire", 
        		"once apon a time there was a bee", "Bill Gates", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Wilson Gilmoore", "The Digital Dived", 
        		"once apon a time there was a bee", "Wilson Gilmoore", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Steve Jones", "E Waste", 
        		"once apon a time there was a bee", "Steve JOnes", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Willy Williams", "Computer Screens and eye Development", 
        		"once apon a time there was a bee", "Willy Williams", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Opra Winfrey", "The Femputer", 
        		"If I can do it you can do it", "Opera Winfrey", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Migule Forne", "LED to Display", 
        		"once apon a time there was a bee", "Migule Forne", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Raj Rohit", "Statistical Machine Learning", 
        		"once apon a time there was a bee", "Raj Rohit", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Denzel Washington", "The Simulator Takes Flight", 
        		"once apon a time there was a bee", "Denzel Washington", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Daniel Tosh", "Behind the Green Screen", 
        		"once apon a time there was a bee", "Daniel Tosh", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf.addPaperToConference("Camron Jones", "Network Models", 
        		"once apon a time there was a bee", "Camron Jones", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","new your living");
        conf.addPaperToConference("Kumar Parashar", "Virtualization", 
        		"once apon a time there was a bee", "Camron Jones", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","new your living");
        conf.addPaperToConference("Nora Vrosted", "Women In Computer Ethics", 
        		"once apon a time there was a bee", "Nora Vrolsted", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","new your living");
        int j = 0;
		for(ConferencePaper the_paper : conf.getConferencePapers()) {
            j++;
                if (j == 1)
                {
                    continue;
                }
    
                conf.getProgramChair().assignManuscript(sub_chair, the_paper);
                if (j>3)
                {
                    break;
                }
			}
			conf.designateReviewer("Kevin Brown");
			conf.designateReviewer("Sean Gill");
			conf.designateReviewer("Shina Grail");
			conf.designateReviewer("James Smith");
			conf.designateReviewer("Woody Allen");
			conf.designateReviewer("Darius Moore");
			conf.designateReviewer("Kevin Wilson");
			conf.designateReviewer("James Evens");
			conf.designateReviewer("Bill Collison");
			conf.designateReviewer("Mandy Simmons");
			conf.designateReviewer("Nancy Drew");
			conf.designateReviewer("Kalyeen Davis");
			conf.designateReviewer("Mike Mullen");
			conf.designateReviewer("Albert Einstein");
			conf.designateReviewer("Nancy Harris");
			conf.designateReviewer("Nathan Daniels");
			conf.designateReviewer("Jamelle Horne");
			conf.designateReviewer("Durwin Davis");
			conf.designateReviewer("Jonny Matthews");
			conf.designateReviewer("Picaso White");
			conf.designateReviewer("Will Smith");
			conf.designateReviewer("Xavier Black");
			conf.designateReviewer("Jaleesa Green");
			conf.designateReviewer("Billy Motzart");
        default_portal.addConference(conf);
        
		//second conference
        Conference conf2 = new Conference("MOBICOM - Mobile Computing and Networking", "Steve Wozniak");
        conf2.setDeadline("3/9/2013");
        conf2.designateReviewer("Lawrence Grass");
        conf2.getProgramChair().designateSubprogramChair("Lawrence Grass");
        SubProgramChair sub_chair2 = conf2.getProgramChair().getSubprogramChairs().get("Lawrence Grass");
        
        conf2.addPaperToConference(
        		"Lawrence Grass", "Mobile Gaming for the Future", 
        		"We will like to play more of them", "Lawrence Grass", 
        		"bee@gmail.com","mobile computing",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Sachin Katti", "Achieving single channel, full duplex wireless communication", 
        		"This paper discusses the design of a single channel full-duplex wireless transceiver.",
        		"Sachin Katti", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Danilo De Donno", 
        		"Challenge: towards distributed RFID sensing with software-defined radio", 
        		"In this paper we discuss an alternative approach, where receive functions are performed by a separate device called \"RFID listener\".",
        		"Danilo De Donno", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Aaron Schulman", "Bartendr: a practical approach to energy-aware cellular data scheduling", 
        		"Cellular radios consume more power and suffer reduced data rate when the signal is weak. ", "Aaron Schulman", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Aaron Schulman", "ZiFi: wireless LAN discovery via ZigBee interference signatures", 
        		"WiFi networks have enjoyed an unprecedent penetration rate in recent years.", "Aaron Schulman", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Aaron Schulman", "Indoor localization without the pain ", 
        		"In this paper, we ask the question: can we perform indoor localization with no pre-deployment effort?",
        		"Aaron Schulman", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Ashwin Ashok", "Challenge: mobile optical networks through visual MIMO", 
        		"Mobile optical communications has so far largely been limited to short ranges of about ten meters", 
        		"Ashwin Ashok", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Souvik Sen", "CSMA/CN: carrier sense multiple access with collision notification", 
        		"A wireless transmitter learns of a packet loss, infers collision, only after completing the entire transmission.",
        		"Souvik Sen", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Davide Zanetti", "Physical-layer identification of UHF RFID tags", 
        		"In this work, we study physical-layer identification of passive UHF RFID tags.",
        		"Davide Zanetti", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Aditya Dhananjay", "Hermes: data transmission over unknown voice channels", 
        		"In this paper, we present Hermes1, a point-to-point data connectivity solution", "Aditya Dhananjay", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
        conf2.addPaperToConference("Chris Y. T. Ma", "Privacy vulnerability of published anonymous mobility traces", 
        		"once apon a time there was a bee", "Chris Y. T. Ma", 
        		"bee@gmail.com","wildlife",
        		"bee, nature, horror","bees are facinating");
     
        int i = 0;
		for(ConferencePaper the_paper : conf2.getConferencePapers()) {
		        i++;
		        if (i == 1)
		        {
		            continue;
		        }
				conf2.getProgramChair().assignManuscript(sub_chair2, the_paper);
				if (i>3)
				{
				    break;
				}
			}
			conf2.designateReviewer("Kevin Brown");
			conf2.designateReviewer("Sean Gill");
			conf2.designateReviewer("Shina Grail");
			conf2.designateReviewer("James Smith");
			conf2.designateReviewer("Woody Allen");
			conf2.designateReviewer("Darius Moore");
			conf2.designateReviewer("Kevin Wilson");
			conf2.designateReviewer("James Evens");
			conf2.designateReviewer("Bill Collison");
			conf2.designateReviewer("Mandy Simmons");
			conf2.designateReviewer("Nancy Drew");
			conf2.designateReviewer("Kalyeen Davis");
			conf2.designateReviewer("Mike Mullen");
			conf2.designateReviewer("Albert Einstein");
			conf2.designateReviewer("Nancy Harris");
			conf2.designateReviewer("Nathan Daniels");
			conf2.designateReviewer("Jamelle Horne");
			conf2.designateReviewer("Durwin Davis");
			conf2.designateReviewer("Jonny Matthews");
			conf2.designateReviewer("Picaso White");
			conf2.designateReviewer("Will Smith");
			conf2.designateReviewer("Xavier Black");
			conf2.designateReviewer("Jaleesa Green");
			conf2.designateReviewer("Billy Motzart");
        default_portal.addConference(conf2);
        
       // default_portal.getConference("The Perils of Java Swing");
        return default_portal;
    }


}
